package com.gaowl.yygh.user.api;

import com.gaowl.yygh.commom.result.Result;
import com.gaowl.yygh.commom.utils.AuthContextHolder;
import com.gaowl.yygh.model.user.Patient;
import com.gaowl.yygh.user.service.PatientService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @program: appointment-register
 * @description: 就珍人管理API
 * @author: gaowl@stu.jiangnan.edu.cn
 * @create: 2021-04-06 18:25
 **/
@RestController
@RequestMapping("/api/user/patient")
public class PatientApiController {

    @Autowired
    private PatientService patientService;

    /*
    * @Description: 获取就诊人列表
    * @Author: gaowl@stu.jiangnan.edu.cn
    * @Date: 2021/04/07
    */
    @GetMapping("auth/findAll")
    public Result findAll(HttpServletRequest request){
        Long userId = AuthContextHolder.getUserId(request);
        List<Patient> list = patientService.findAllUserId(userId);
        return Result.ok(list);
    }

    /*
     * @Description: 添加就诊人
     * @Author: gaowl@stu.jiangnan.edu.cn
     * @Date: 2021/04/07
     */
    @PostMapping("auth/save")
    public Result savePatient(@RequestBody Patient patient, HttpServletRequest request){
        Long userId = AuthContextHolder.getUserId(request);  // 获取当前用户的id
        patient.setUserId(userId);
        patientService.save(patient);
        return Result.ok();
    }

    /*
     * @Description: 根据id获取就诊人信息
     * @Author: gaowl@stu.jiangnan.edu.cn
     * @Date: 2021/04/07
     */
    @GetMapping("auth/get/{id}")
    public Result getPatient(@PathVariable Long id) {
        Patient patient = patientService.getPatientId(id);
        return Result.ok(patient);
    }

    @GetMapping("inner/get/{id}")
    public Patient getPatientOrder(@PathVariable("id") Long id){
        return patientService.getById(id);
    }

    /*
     * @Description: 修改就诊人信息
     * @Author: gaowl@stu.jiangnan.edu.cn
     * @Date: 2021/04/07
     */
    @PostMapping("auth/update")
    public Result updatePatient(@RequestBody Patient patient) {
        System.out.println(patient);
        patientService.updateById(patient);
        return Result.ok();
    }

    /*
     * @Description: 根据id删除就诊人
     * @Author: gaowl@stu.jiangnan.edu.cn
     * @Date: 2021/04/07
     */
    @DeleteMapping("auth/remove/{id}")
    public Result removePatient(@PathVariable Long id) {
        patientService.removeById(id);
        return Result.ok();
    }
}
